#!/usr/bin/env python
import os
import sys
import django

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'boss.settings')
django.setup()

from qa.views import MutChatAPIView
from django.test import RequestFactory
import json

def test_qa_view():
    print("=== 测试QA多轮对话视图 ===")
    
    # 创建请求工厂
    factory = RequestFactory()
    
    # 第一次对话
    print("\n1. 第一次对话")
    request1 = factory.post('/qa/mutchat/', 
                           data=json.dumps({"message": "你好，我叫张三"}),
                           content_type='application/json')
    
    view = MutChatAPIView()
    response1 = view.post(request1)
    
    if response1.status_code == 200:
        data1 = response1.data
        conversation_id = data1['conversation_id']
        print(f"会话ID: {conversation_id}")
        print(f"AI回复: {data1['answer']}")
        print(f"消息数量: {len(data1['messages'])}")
    else:
        print(f"第一次对话失败: {response1.status_code}")
        return
    
    # 第二次对话
    print("\n2. 第二次对话")
    request2 = factory.post('/qa/mutchat/', 
                           data=json.dumps({
                               "conversation_id": conversation_id,
                               "message": "我刚才问的什么？"
                           }),
                           content_type='application/json')
    
    response2 = view.post(request2)
    
    if response2.status_code == 200:
        data2 = response2.data
        print(f"AI回复: {data2['answer']}")
        print(f"消息数量: {len(data2['messages'])}")
    else:
        print(f"第二次对话失败: {response2.status_code}")

if __name__ == "__main__":
    test_qa_view()
